const app = getApp();
let animation = wx.createAnimation({
  duration: 4000, //转盘转动时间
  timingFunction: 'ease-out' //动画效果
})
Page({
  data: {
    openid: '',

    rotate: 0, //度数
    turning: false, //是否点击,
    prize: '', //中奖概率
    count: '', //剩余次数
    prizeList: [
      2100, //一等奖 ...
      2400, //二等奖  ...
      2330, //三等奖...
      2610, //参与奖 ...
      2200, //谢谢惠顾 ...
    ],
    //向1006传递的数据
    USER_PRIZE_ID: '',
    modalHidden: true, //新的图片模态框
    CUSTOMER_ID: '',
    ad_img: '',
    flag: true, //设置节流
    USER_ASSETS_IDCARD:'',
    PRIZE_ID: '',
    WALKING_ID: '',
  },


  backshang() {
    wx.navigateBack({
      delta: 1,
    })
  },




  start() {
    let that = this
    // // 节流
    if (that.data.flag) {
      that.data.flag = false
      wx.request({
        url: app.globalData.interface+'/xianPost/API_6053_xianguapaiRaffle',
        method: 'POST',
        data: {
          USER_ASSETS_IDCARD: this.data.USER_ASSETS_IDCARD,
          OPEN_ID: app.globalData.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          if (res.data.result == "error") {
            wx.showModal({
              title: '您已抽过',
              content: '抽中'+res.data.prize.PRIZE_NAME+':'+res.data.prize.PRIZE_EXPLAIN,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后
                  console.log('用户点击确定')
                } else { //这里是点击了取消以后 
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            console.log(res.data);
            console.log(res.data.prizeMessage.STOCK_ID);
            that.setData({
              PRIZE_ID: res.data.prizeMessage.PRIZE_ID,
     
              prize:res.data.prizeMessage.PRIZE_ID,
            })
            setTimeout(() => {
              that.setData({
                ad_img: res.data.prizeMessage.STOCK_ID
              })
            }, 4800)
            console.log(res);
 
            const index_ = res.data.prizeMessage.PRIZE_ID;
            that.rotate(index_)
          }
        }
      })
      setTimeout(() => {
        that.data.flag = true
      }, 4800)
    }


  },

  rotate: function (index_) {
    let _this = this;
    // let cat = 60;
    if (!this.data.turning) {
      let rdm = 0; //度数
      rdm = Math.floor(Math.random() * 3600); //最大10圈
      // rdm = 2100; //最大10圈
      //用索引的方法对应角度
      rdm = this.data.prizeList[index_ - 1]; //直接确定转的度数 
      // rdm =2200;//直接确定转的度数 
      animation.rotate(rdm).step();
      this.setData({
        rotate: animation.export(),
        turning: true
      });
      setTimeout(() => {
        this.setData({
          turning: false
        });
        let num = rdm % 360; //转了多少度
        //console.log(num);
        function showModal(str) {
          wx.showModal({
            title: '恭喜抽中',
            content: str,
            success: function () { //点击确定后还原到0度
              let animation = wx.createAnimation({
                duration: 100,
                timingFunction: 'ease-out'
              });
              animation.rotate(0).step();
              _this.setData({
                rotate: animation.export(),
                turning: false
              });
            },
            fail: function (res) {
              console.log(res)
            }
          })
        }

        if (rdm == 2200) {
          // showModal('谢谢惠顾');
          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2100) {
          // showModal('一等奖 ');
          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2330) {
          // showModal('三等奖');
          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2610) {
          // showModal('参与奖');
          this.setData({
            modalHidden: false
          })
        } else if (rdm == 2400) {
          // showModal('二等奖');
          this.setData({
            modalHidden: false
          })
        }
      }, 4000)
    }
  },






  showPopup() {
    this.setData({
      modalHidden: true,
    })
  },











  onLoad: function (e) {
    console.log(e);
    let topshu = JSON.parse(e.data)
    console.log(topshu);

    this.setData({
      USER_ASSETS_IDCARD: topshu
    })
  },


  onShow() {
    this.setData({
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
      CUSTOMER_ID: app.globalData.CUSTOMER_ID,
    });
  },

})